package com.vsd.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.demo.dialet.PaginationSupport;
import com.demo.util.MathUtil;
import com.demo.util.SqlUtils;
import com.demo.util.StringUtil;
import com.vsd.daoEx.model.IndexActGoodsExWithGoods;
import com.vsd.domain.IndexActGoods;
import com.vsd.repository.IndexActGoodsRepository;
import com.vsd.service.IndexActGoodsService;
import com.zy.store.proto.MOAppStore.MTgGoodsMini;
import com.zy.store.proto.MOAppStore.MTgGoodsMiniList;

@Service
@Transactional
public class IndexActGoodsServiceImpl implements IndexActGoodsService {

	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory
			.getLogger(IndexActGoodsServiceImpl.class);
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Autowired
	private IndexActGoodsRepository  indexActGoodsRepository;

	@Override
	public MTgGoodsMiniList.Builder selectAppList(String actId,String lat,String lng,
			Integer page, Integer limit) {
		MTgGoodsMiniList.Builder ret = MTgGoodsMiniList.newBuilder();
		
		
		String sql = "select sg.id as goodsId,sg.`name` as goodsName,sg.img,sg.now_price,sg.sell_base	,s.`name` as storeName ";
		
		if(StringUtils.isNoneBlank(lat)&&StringUtils.isNoneBlank(lng)){
			sql+=",getDistance(s.lng,s.lat,"+lng+","+lat+") as distance";
		}
		
		
		sql+= "	,(select count(1) from s_order o where o.store_id = s.id and o.state=4) as sellCnt"
			+ "	from f_index_act_goods ag  left join s_store_goods sg on ag.gid = sg.id"
			+ "	left join s_store s on s.id = sg.store_id"
			+ "	where s.is_online=1 and s.closed=0 and sg.is_online=1 and sg.is_del=0 and ag.act_id = '"+ actId+"' order by sort_order desc"
			+ " limit "+page*limit+","+(page+1)*limit;
		
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		
		for (Map<String, Object> map : maps) {
			MTgGoodsMini.Builder mini = MTgGoodsMini.newBuilder();
			mini.setDistance(MathUtil.getDistanceStr(StringUtil.obj2String(map.get("distance"))));
			mini.setGoodsName(StringUtil.obj2String(map.get("goodsName")));
			mini.setId(StringUtil.obj2String(map.get("goodsId")));
			mini.setImg(StringUtil.obj2String(map.get("img")));
			mini.setPrice(StringUtil.obj2String(map.get("now_price")));
			int sellBase = map.get("sell_base")==null?0:new Integer(map.get("sell_base").toString());
			int sellCnt = map.get("sellCnt")==null?0:new Integer(map.get("sellCnt").toString());
			mini.setSellCnt((sellBase+sellCnt)+"");
			mini.setStoreName(StringUtil.obj2String(map.get("storeName")));
			ret.addList(mini);
		}
		
		return ret;
	}

	@Override
	public List<IndexActGoodsExWithGoods> selectExList(String areaCode) {
		// TODO Auto-generated method stub
		String sql = "select ag.*,sg.`name` as goodsName,sg.img,sg.imgs,sg.now_price as nowPrice"
				+ ",sg.is_online as isOnline,s.`name` as storeName,sg.mode,ia.title,sg.type as goodsType "
			+ "	from f_index_act_goods ag  left join s_store_goods sg on ag.gid = sg.id"
			+" left join f_index_activity ia on ia.id = ag.act_id"
			+ "	left join s_store s on s.id = sg.store_id"
			+ "	where sg.is_del=0 ";
		if(StringUtils.isNotBlank(areaCode)){
			sql+=" and ia.area_code = '"+areaCode+"'";
		}
		sql+=" order by ag.create_time desc";
		
		int c = jdbcTemplate.queryForObject("select count(1) from ("+ sql+") a_cnt", Integer.class);
		PaginationSupport.getContext().setTotalCount(c);
		
		sql+=" limit "+(PaginationSupport.getContext().getStartRowNo()-1)+","+PaginationSupport.getContext().getPageLength();
		
		
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		List<IndexActGoodsExWithGoods> exList = new ArrayList<>();
		for (Map<String, Object> map : maps) {
			IndexActGoodsExWithGoods ex = new IndexActGoodsExWithGoods();
			SqlUtils.transMap2Bean2(map, ex);
			exList.add(ex);
		}
		
		return exList;
	}
	@Override
	public void delById(String id) {
		// TODO Auto-generated method stub
		indexActGoodsRepository.delete(id);
	}
	@Override
	public int updateByPrimaryKey(IndexActGoods goods) {
		// TODO Auto-generated method stub
		try {
			String sql = SqlUtils.update(goods, true, " where id = '"+goods.getId()+"'");
			return jdbcTemplate.update(sql);
		} catch ( Exception e) {
			return 0;
		}
	}

	@Override
	public IndexActGoods findByGoodsIdAndActId(String gId, String actId) {
		// TODO Auto-generated method stub
		return indexActGoodsRepository.findByGidAndActId(gId, actId);
	}

	@Override
	public void save(IndexActGoods goods) {
		// TODO Auto-generated method stub
		indexActGoodsRepository.save(goods);
	}

	@Override
	public List<IndexActGoodsExWithGoods> selectExList(String actId,String storeId) {
		// TODO Auto-generated method stub
		String sql = "select ag.*,sg.`name` as goodsName,sg.img,sg.imgs,sg.now_price as nowPrice"
				+ ",sg.is_online as isOnline,s.`name` as storeName,sg.mode,ia.title,sg.type as goodsType "
			+ "	from f_index_act_goods ag  left join s_store_goods sg on ag.gid = sg.id"
			+" left join f_index_activity ia on ia.id = ag.act_id"
			+ "	left join s_store s on s.id = sg.store_id"
			+ "	where sg.is_del=0 and sg.store_id = '"+storeId+"'";
		if(StringUtils.isNotBlank(actId)){
			sql+=" and ia.id = '"+actId+"'";
		}
		
		sql+=" order by ag.create_time desc";
		
		int c = jdbcTemplate.queryForObject("select count(1) from ("+ sql+") a_cnt", Integer.class);
		PaginationSupport.getContext().setTotalCount(c);
		
		sql+=" limit "+(PaginationSupport.getContext().getStartRowNo()-1)+","+PaginationSupport.getContext().getPageLength();
		
		
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		List<IndexActGoodsExWithGoods> exList = new ArrayList<>();
		for (Map<String, Object> map : maps) {
			IndexActGoodsExWithGoods ex = new IndexActGoodsExWithGoods();
			SqlUtils.transMap2Bean2(map, ex);
			exList.add(ex);
		}
		
		return exList;
	}

 

	
}
